#include "mapTextureLoaders.h"

std::vector< sf::Image > loadAllTexture( std::vector<std::string>& fileNames, std::string subDirectory = "" )
{
	//this is the vector which will be returned
	std::vector< sf::Image > images;
	//loop through all the files names passed in
	for(unsigned int i = 0; i < fileNames.size(); i++ )
	{
		//this string is used to check the extention of the file name
		//so that we don't get files like the . .. thumb.db or any
		//other none image file
		std::string extention;
		//this string gets us to the . part of the file name
		//so that we can test the extention part
		std::string testString = fileNames[i];
		unsigned int j;
		//loop till we see a .
		for( j = 0; testString[j] != '.'; j++ )
		{
		}
		//set teh extention to the . and every thing after it
		//i.e. extention = '.png', '.gif' or '..'
		for(; j < testString.size(); j++ )
		{
			extention.push_back( testString[j] );
		}
		//check if it is an image extention. we only do 
		//bmp, dds, jpg, pcx, png, pnm, raw, sgi, tga, tif
		//because that is all SFML supports
		if( extention.compare( ".png" ) == NULL || extention.compare( ".jpg" ) == NULL ||
			extention.compare( ".bmp" ) == NULL || extention.compare( ".dds" ) == NULL ||
			extention.compare( ".pcx" ) == NULL || extention.compare( ".pnm" ) == NULL ||
			extention.compare( ".raw" ) == NULL || extention.compare( ".sgi" ) == NULL ||
			extention.compare( ".tga" ) == NULL || extention.compare( ".tif" ) == NULL)
		{
			// creates a string Like: 'Textures\blue.png'
			// so s.load_image() can read the file
			std::string temp;
			if( subDirectory.compare( "" ) != NULL )
			{
				temp = subDirectory + "\\" + fileNames[i].c_str();
			}
			else
			{
				temp = fileNames[i].c_str();
			}

			// loads the image in the directory like: 'Graphics\blue.png'
			sf::Image img;
			img.LoadFromFile( temp );
			//store in the images vector for returning
			images.push_back( img );
		}
	}
	//return all the images we have loaded
	return images;
}

std::vector< sf::Image > loadMapTextures( std::vector<std::string>& textureList, std::vector<std::string>& fileNames, std::string subDirectory )
{
	std::vector< sf::Image > images;
	for( unsigned int i = 0; i < textureList.size(); ++i )
	{

		//check through folder for file names to be loaded
		//since we are checking against file names in the 
		//map text file we don't need to worry about files
		//like . .. thumb.db being loaded because they will
		//just be skipped if they don't match one of the names 
		//in the map text file
		unsigned int j;
		for( j = 0; j < fileNames.size() && textureList[i].compare( fileNames[j] ) != 0; ++j )
		{
		}
		// check to see that i is less than the size of vec
		// if it is the size of vec then the file doesn't 
		// exist and the function exits.
		if( j == fileNames.size()  )
		{
			//Debugging Code
			std::cout << "Missing file: " << textureList[i] << " Check the case of the file's Extention " << std::endl << "i.e. .PNG -> .png " << std::endl;
			exit(1);
		}
		else
		{
			// creates a string Like: 'Textures\blue.png'
			// so s.load_image() can read the file
			std::string temp;
			if( subDirectory.compare( "" ) != NULL )
			{
				temp = subDirectory + "\\" + fileNames[j].c_str();
			}
			else
			{
				temp = fileNames[j].c_str();
			}

			// loads the image in the directory like: 'Graphics\blue.png'
			sf::Image img;
			img.LoadFromFile( temp );
			images.push_back( img );
		}
	}
	return images;
}

std::vector<std::string> lineUpFileNames( std::vector<std::string>& fileNames )
{
//loop through all the files names passed in
	std::vector< std::string > orderedList;
	for(unsigned int i = 0; i < fileNames.size(); i++ )
	{
		//this string is used to check the extention of the file name
		//so that we don't get files like the . .. thumb.db or any
		//other none image file
		std::string extention;
		//this string gets us to the *. part of the file name
		//so that we can test the extention part
		std::string testString = fileNames[i];
		unsigned int j;
		//loop till we see a .
		for( j = 0; testString[j] != '.'; j++ )
		{
		}
		//set the extention to the . and every thing after it
		//i.e. extention = '.png', '.gif' or '..'
		for(; j < testString.size(); j++ )
		{
			extention.push_back( testString[j] );
		}
		//check if it is an image extention. we only do 
		//bmp, dds, jpg, pcx, png, pnm, raw, sgi, tga, tif
		//because that is all SFML supports
		if( extention.compare( ".png" ) == NULL || extention.compare( ".jpg" ) == NULL ||
			extention.compare( ".bmp" ) == NULL || extention.compare( ".dds" ) == NULL ||
			extention.compare( ".pcx" ) == NULL || extention.compare( ".pnm" ) == NULL ||
			extention.compare( ".raw" ) == NULL || extention.compare( ".sgi" ) == NULL ||
			extention.compare( ".tga" ) == NULL || extention.compare( ".tif" ) == NULL)
		{
			orderedList.push_back( fileNames[i] );
		}
	}
	return orderedList;
}

